<?php
/**
 * Created by PhpStorm.
 * User: lullabies
 * Date: 2019/5/29
 * Time: 11:24
 */

namespace app\admin\controller;
use app\Admin\model\Works;
use app\Admin\validate\ExamSubject;
use app\Admin\model\Subjects as SubjectsModel;
use app\Admin\model\Grade as GradeModel;
use app\Admin\validate\GetSortDetail;
use app\Admin\model\Works as WorksModel;

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,fs-token");
header('Access-Control-Allow-Methods: GET, POST');
/**
 * @title 单科分数管理
 * @group Admin
 */
class Grade extends Base
{
     /**
      * @title 根据考试id和科目名称 获得评分作品分数段
      * @author 开发者
      * @url /admin/grade/getSortCount
      * @method POST/GET
      * @description 测试的考试id为1 科目名称色彩
      * @param name:exam_id require:1 default:null type:int desc:考试id
      * @param name:name require:1 default:null type:varchar desc:科目名称
      * @return name:分段名称
      * @return count:当前分段的人数
      * @return percent:当前分段百分比
      * @return  subject_id:当前科目id
      */
    public function getSortCount(){
        $data = I('exam_id|name');
        (new ExamSubject())->goCheck();
        //根据考试id 可科目名称获取科目id
        $subject = SubjectsModel::where($data)->find();
        $subject_id = $subject['id'];
        if(!$subject_id)$this->apiReturn(303,'未查询到相关科目！');
        //根据科目ID 获取已打分用户总数
        $result = GradeModel::where('subject_id',$subject_id)->count();
        //获取分数在95分以上的人数
        $ninef="score >= 95 ";
        $ninef= GradeModel::getSortCount($subject_id,$ninef);
        //获取分数在90分以上的人数
        $nine="score >= 90 and score < 95";
       $nine = GradeModel::getSortCount($subject_id,$nine);

        //获取分数在85分以上的人数
        $eightf="score >= 85 and score < 90 ";
        $eightf= GradeModel::getSortCount($subject_id,$eightf);
        //获取分数在80分以上的人数
        $eight="score >= 80 and score < 85";
        $eight = GradeModel::getSortCount($subject_id,$eight);

        //获取分数在75分以上的人数
        $sevenf="score >= 75 and score < 70 ";
        $sevenf= GradeModel::getSortCount($subject_id,$sevenf);
        //获取分数在70分以上的人数
        $seven="score >= 70 and score < 75";
        $seven = GradeModel::getSortCount($subject_id,$seven);

        //获取分数在65分以上的人数
        $sixf="score >= 65 and score < 70 ";
        $sixf= GradeModel::getSortCount($subject_id,$sixf);
        //获取分数在60分以上的人数
        $six="score >= 60 and score < 65";
        $six = GradeModel::getSortCount($subject_id,$six);

        //获取分数在60分以下的人数
        $underSix = GradeModel::getSortCount($subject_id,"score<60");

        $arr = array(
            array('name'=>'95分以上','count'=>$ninef),
            array('name'=>'90分以上','count'=>$nine),
            array('name'=>'85分以上','count'=>$eightf),
            array('name'=>'80分以上','count'=>$eight),
            array('name'=>'75分以上','count'=>$sevenf),
            array('name'=>'70分以上','count'=>$seven),
            array('name'=>'65分以上','count'=>$sixf),
            array('name'=>'60分以上','count'=>$six),
            array('name'=>'60分以下','count'=>$underSix),
        );
        $data = array();
        foreach ($arr as $k=>$v){
            //获取每个分段的百分比
            $data[$k]=$v;
           $data[$k]['percent'] = $v['count']/$result;
        }
        $data[]['subject_id']=$subject_id;
        $this->apiReturn(200,'查询成功！',$data);
    }
    /**
     * @title 根据科目id 获得某分数段作品详情
     * @author 开发者
     * @url /admin/grade/getSortDetail
     * @method POST/GET
     * @description 测试的科目id为1
     * @param name:subject_id require:1 default:null type:int desc:科目id
     * @param name:condition require:1 default:null type:varchar desc:查询条件，即当前分段例如95分以上gt95,60分以下lt60
     * @param name:page require:0 default:1 type:int desc:当前页面数  默认第一页
     * @param name:page_size require:0 default:10 type:int desc:每页显示记录数 默认10条
     * @param name:sort require:0 default:create_time type:vatchar desc:排序关键字 默认打分的先后顺序
     * @param name:order require:0 default:desc type:varchar desc:默认排序为升序
     * @return id:作品id
     * @return img:作品图片
     * @return subject_id:科目id
     * @return user_id:用户id
     * @return create_time:创建时间
     * @return　score:分数
     */
    public function getSortDetail(){
        $subject_id = input('subject_id');
        $condition = input('condition');
        //验证参数
        (new GetSortDetail())->goCheck();
        //判断条件获得查询条件
        switch ($condition){
            case 'gt95':
                $where = 'score >=95';
                break;
            case 'gt90':
                $where = 'score >=90 and score <95';
                break;
            case 'gt85':
                $where = 'score >=85 and score <90';
                break;
            case 'gt80':
                $where = 'score >=80 and score <85';
                break;
            case 'gt75':
                $where = 'score >=75 and score <80';
                break;
            case 'gt70':
                $where = 'score >=70 and score <75';
                break;
            case 'gt65':
                $where = 'score >=65 and score <70';
                break;
            case 'gt60':
                $where = 'score >=60 and score <65';
                break;
            case 'lt60':
                $where = 'score <60';
                break;
        }
        //获得前端显示的当前页数
        $page = input('page', 1, 'intval');
        //让前端设置数据一页显示几条数据
        $page_size = input('page_size', 10, 'intval');
        //排序的关键字
        $sort = input('sort','update_time');
        //显示顺序
        $order = input('order','desc');
        //根据条件查询分数详情
        $data = GradeModel::where('subject_id',$subject_id)->where($where)
            ->page($page,$page_size)
            ->order('create_time','desc')
            ->select();
        //获得总数
        $count = GradeModel::getSortCount($subject_id,$where);
        if(!$count)$this->apiReturn(303,'未查询到相关数据！');
        $result=array();
        foreach ($data as $k=>$v){
            //根据用户id 和科目id 查询作品信息
            $result[]= WorksModel::where('subject_id',$subject_id)->where('user_id',$v['user_id'])->find();
            //将分数添加到结果中
            $result[$k]['score']=$v['score'];
        }
        $this->apiReturn(200,'',$result,$count);
    }
    /**
     * @title 根据作品id 添加或修改作品分数
     * @author 开发者
     * @url /admin/grade/editGrade
     * @method POST/GET
     * @description 测试的作品id为1
     * @param name:id require:1 default:null type:int desc:作品id
     * @param name:score require:1 default:null type:int desc:作品分数
     */
    public function editGrade(){
        $data =I('id|score');
        foreach ($data as $v){
            if(!$v)$this->apiReturn(303,'缺少相关参数！');
        }
        //查询作品相关信息
        $works = WorksModel::get($data['id']);
        //通过项目id和用户id 获取分数记录
        $grade = GradeModel::where(['subject_id'=>$works['subject_id'],'user_id'=>$works['user_id']])->find();
        //更新分数并保存
        $grade->score=$data['score'];
        if($grade->force()->save()){
            $this->apiReturn(200,'评分成功！');
        }else{
            $this->apiReturn(303,'评分失败！');
        }
    }
}